package com.insight.modules.monitor.controller;

import lombok.extern.slf4j.Slf4j;
import com.insight.common.api.vo.Result;
import com.insight.modules.monitor.domain.RedisInfo;
import com.insight.modules.monitor.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys/actuator/redis")
public class ActuatorRedisController {

	@Autowired
	private RedisService redisService;

	/**
	 * 获取Redis服务器的详细信息
	 *
	 * @return 返回Redis服务器信息列表，封装在Result中
	 * @throws Exception 可能抛出的异常
	 */
	@GetMapping("/info")
	public Result<?> getRedisInfo() throws RedisConnectException {
		List<RedisInfo> infoList = this.redisService.getRedisInfo();
		log.info(infoList.toString());
		return Result.ok(infoList);
	}

	/**
	 * 获取Redis中所有库的键数量统计
	 *
	 * @return 返回一个Map，key为库编号，value为对应的键数量
	 * @throws Exception 可能抛出的异常
	 */
	@GetMapping("/keysSize")
	public Map<String, Object> getKeysSize() throws RedisConnectException {
		return redisService.getKeysSize();
	}

	/**
	 * 获取Redis内存使用情况
	 *
	 * @return 返回一个Map，包含内存相关信息
	 * @throws Exception 可能抛出的异常
	 */
	@GetMapping("/memoryInfo")
	public Map<String, Object> getMemoryInfo() throws RedisConnectException {
		return redisService.getMemoryInfo();
	}

	// update-begin--Author:zhangweijian  Date:20190425 for：获取磁盘信息

	/**
	 * 获取服务器磁盘信息
	 *
	 * @param request  HttpServletRequest对象
	 * @param response HttpServletResponse对象
	 * @return Result封装的磁盘信息列表，每条包含磁盘名称、总容量、剩余容量和使用百分比
	 */
	@GetMapping("/queryDiskInfo")
	public Result<List<Map<String,Object>>> queryDiskInfo(HttpServletRequest request, HttpServletResponse response){
		Result<List<Map<String,Object>>> res = new Result<>();
		try {
			FileSystemView fsv = FileSystemView.getFileSystemView();
			File[] fs = File.listRoots();
			if (fs == null) {
				res.setResult(new ArrayList<>());
				res.success("查询成功");
				return res;
			}
			log.info("查询磁盘信息:" + fs.length + "个");
			List<Map<String,Object>> list = new ArrayList<>();

			for (int i = 0; i < fs.length; i++) {
				if (fs[i] == null) {
					continue;
				}
				long totalSpace = fs[i].getTotalSpace();
				if (totalSpace == 0) {
					continue;
				}
				Map<String,Object> map = new HashMap<>();
				if (fsv != null) {
					map.put("name", fsv.getSystemDisplayName(fs[i]));
				} else {
					map.put("name", fs[i].getPath());
				}
				map.put("max", totalSpace);
				long freeSpace = fs[i].getFreeSpace();
				map.put("rest", freeSpace);
				if (totalSpace > 0) {
					long usedSpace = totalSpace - freeSpace;
					map.put("restPPT", (int) (usedSpace * 100 / totalSpace));
				} else {
					map.put("restPPT", 0);
				}
				list.add(map);
				log.info(map.toString());
			}
			res.setResult(list);
			res.success("查询成功");
		} catch (Exception e) {
			res.error500("查询失败" + e.getMessage());
		}
		return res;
	}

	// update-end--Author:zhangweijian  Date:20190425 for：获取磁盘信息
}

